package com.chorye.function.pdf.read.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Bean的工具类
 *
 * @author Chorye
 */
public class BeanUtil {

    private BeanUtil() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * 日志工具
     */
    private static final Logger log = LoggerFactory.getLogger(BeanUtil.class);

    /**
     * 设置字段值  如：username 字段,setUsername(String username)
     *
     * @param target 要设置的对象
     * @param name   字段名称
     * @param type   字段类型
     * @param value  字段值
     */
    public static void setFieldValue(Object target, String name, Class type,
                                     Object value) {
        if (target == null
                || name == null
                || "".equals(name)
                //如果类型不匹配，直接退出
                || (value != null && !type.isAssignableFrom(value.getClass()))) {
            return;
        }
        Class clazz = target.getClass();
        try {  //先通过setXxx()方法设置类属性值
            String methodname = "set" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
            //获取定义的方法
            Method[] declaredMethods = clazz.getDeclaredMethods();
            for (int i = 0; i < declaredMethods.length; i++) {
                Method method = declaredMethods[i];
                String methodName = method.getName();
                if (methodName.equals(methodname)) {
                    //设置非共有方法权限
                    if (!Modifier.isPublic(method.getModifiers())) {
                        method.setAccessible(true);
                    }
                    if (value != null) {
                        //执行方法回调
                        if (method.getParameterTypes()[0].getTypeName().contains("BigDecimal")) {
                            value = BigDecimal.valueOf(Double.valueOf((String) value));
                        } else if (method.getParameterTypes()[0].getTypeName().contains("Integer")) {
                            value = Integer.valueOf((String) value);
                        } else if (method.getParameterTypes()[0].getTypeName().contains("Date")) {
                            value = new Date((String) value);
                        } else if (method.getParameterTypes()[0].getTypeName().contains("Long")) {
                            value = Long.valueOf((String) value);
                        } else if (method.getParameterTypes()[0].getTypeName().contains("Double")) {
                            value = Double.valueOf((String) value);
                        }
                        method.invoke(target, value);
                    }
                    return;
                }
            }
            //如果set方法不存在，则直接设置类属性值
        } catch (Exception me) {
            if (log.isDebugEnabled()) {
                log.debug(me.toString());
            }
            try {
                //获取定义的类属性
                Field field = clazz.getDeclaredField(name);
                //设置非共有类属性权限
                if (!Modifier.isPublic(field.getModifiers())) {
                    field.setAccessible(true);
                }
                //设置类属性值
                field.set(target, value);

            } catch (Exception fe) {
                if (log.isDebugEnabled()) {
                    log.debug(fe.toString());
                }
            }
        }
    }
}
